//
//  FR_TeacherAcountViewController.m
//  FaceRecognition
//
//  Created by syun on 17/2/8.
//  Copyright © 2017年 Sylar. All rights reserved.
//

#import "FR_TeacherAcountViewController.h"
#import "DropTable.h"
#import "ClassTopTableViewCell.h"
#import "AccountListTableViewCell.h"
#import "MyAccountTableViewCell.h"
#import "FR_RankListViewController.h"
#import "ManagerInfoModel.h"
#import "RankModel.h"
@interface FR_TeacherAcountViewController ()<DropTableDelgate,UITableViewDelegate,UITableViewDataSource,UIAlertViewDelegate>
{
    UILabel      *_navTitlelabel;
    UIImageView  *_navImageView;
    UIImageView  *navBackView;
    DropTable * drop_table;
    UIButton * leftButton;
    UIButton * rightButton;
    UIImageView * scrollLine;
    
    NSInteger daysdis;//加减天数
    NSInteger monthdis;//加减月数
    UILabel * dataLabel;//日统计日期
    
    int isdayAcount;//1代表当前日统计 2 代表当前是月统计
    
    NSString * selectedClassId;//当前选中的班级id
    NSString * className;//当前选中的班级名字
    
    NSString * selectedDataTime;//当前日
    NSString * selectedMonthTime;//当前月
    
    int pageNum;
    int MpageNum;
    
    UILabel * noDataLabel;
    
    NSString * currentmonth;
}
@property (nonatomic, strong) UITableView * classTable;

@property (nonatomic, strong) NSArray * dataArr;//请求班级数组
@property (nonatomic, strong) NSMutableArray * classnameArr;//班级名字数组
@property (nonatomic, strong) NSMutableArray * classidArr;//班级对应的ID数组
@property (nonatomic, strong) NSMutableArray * rankArray;//请求排名数组
@property (nonatomic, strong) NSMutableArray * dataArray;//日、月统计请求数组
@end

@implementation FR_TeacherAcountViewController


-(void)viewWillAppear:(BOOL)animated
{
    NSLog(@"rankArray = %@",self.rankArray);
}
- (UITableView *)classTable
{
    if (_classTable == nil) {
        
        _classTable = [[UITableView alloc]initWithFrame:CGRectMake(0,64, SCREENWIDTH, SCREENHEIGH-64) style:UITableViewStylePlain];
        _classTable.delegate = self;
        _classTable.dataSource = self;
        _classTable.separatorStyle = UITableViewCellSeparatorStyleNone;
        [_classTable setShowsVerticalScrollIndicator:NO];
        
    }
    
    return _classTable;
}

- (NSMutableArray *)classnameArr
{
    if (_classnameArr  == nil) {
        
        _classnameArr = [NSMutableArray array];
    }
    
    return _classnameArr;
}

- (NSMutableArray *)classidArr
{
    if (_classidArr == nil) {
        
        _classidArr = [NSMutableArray array];
    }
    
    return _classidArr;
}

- (NSMutableArray *)rankArray
{
    if (_rankArray == nil) {
        
        _rankArray = [NSMutableArray array];
    }
    
    return _rankArray;
}

- (NSMutableArray *)dataArray
{
    if (_dataArray == nil) {
        
        _dataArray = [NSMutableArray array];
    }
    
    return _dataArray;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    daysdis = 0;
    monthdis = 0;
    pageNum = 1;
    MpageNum = 1;
    isdayAcount = 1;//默认日统计
    [self getCurrentData];// 默认当前日期
    self.dataArr = [NSArray array];
    [self requestClasses];
    [self.view addSubview:self.classTable];
    
    // Do any additional setup after loading the view.
}


- (void)dropList
{
    drop_table = [[DropTable alloc] init];
    drop_table.lists = self.classnameArr;
    drop_table.localtion = localtionTypeMiddle;
    drop_table.delegate = self;
}

#pragma mark -DropTableDelegate
//切换班级
- (void)tableViewCellSelected:(NSInteger)index{

    className = [self.classnameArr objectAtIndex:index];
    
    _navTitlelabel.text = className;
    
    selectedClassId = [self.classidArr objectAtIndex:index];
    
    [self requestRankingData];
    
    if (isdayAcount == 1) {
        
        [self requstDateRankData];
    }else
    {
        [self requstMonthRankData];
    }
    
   
}

- (void)setupUI {
    
    navBackView=[[UIImageView alloc]initWithFrame:CGRectMake(SCREENWIDTH/2-60, 20, 120, 44)];
    navBackView.userInteractionEnabled=YES;
    [self.view addSubview:navBackView];
    
    NSString *titleStr = className;
    _navTitlelabel=[[UILabel alloc]initWithFrame:CGRectMake(0,10,120, 18)];
    _navTitlelabel.text = titleStr;
    _navTitlelabel.textAlignment = NSTextAlignmentCenter;
    _navTitlelabel.font = [UIFont boldSystemFontOfSize:18];
    _navTitlelabel.textColor = [UIColor blackColor];
    _navTitlelabel.userInteractionEnabled = YES;
    [navBackView addSubview:_navTitlelabel];
    
    if (self.classnameArr.count >1) {
        
        _navImageView=[[UIImageView alloc]initWithFrame:CGRectMake(navBackView.frame.size.width/2-7.5,CGRectGetMaxY(_navTitlelabel.frame),15, 11)];
        _navImageView.image=[UIImage imageNamed:@"record_head_classchoose_down_"];
        _navImageView.userInteractionEnabled = YES;
        [navBackView addSubview:_navImageView];
    }
    
    UIButton *imgBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    imgBtn.frame = CGRectMake(0, 0, 120, 44);
    [imgBtn addTarget:self action:@selector(selClass:) forControlEvents:(UIControlEventTouchUpInside)];
    [navBackView addSubview:imgBtn];
    
}

- (void)selClass:(UIButton *)sender
{
    [self dropList];
}

#pragma mark - TableViewSoureData

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
        
        return 2;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
        if (section == 0) {
            
            return 1;
        }else
        {
            return self.dataArray.count;
        }
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{

    
        if (indexPath.section == 0) {
            
            return 128;
        }else
        {
            return 44;
        }

    
   

}

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{

    
        if (section == 0) {
            
            return 0;
        }else
        {
            return 131;
        }

}

-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{

        if (section == 0) {
            
            UIView * tBack = [[UIView alloc]initWithFrame:CGRectMake(0, 0, SCREENWIDTH, 10)];
            tBack.backgroundColor = [UIColor colorWithHexString:@"f5f8fa"];
            return tBack;
            
        }else
        {
            UIView * tBack = [[UIView alloc]initWithFrame:CGRectMake(0, 0, SCREENWIDTH, 120)];
            tBack.backgroundColor = [UIColor whiteColor];
            
            UILabel * separateLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, SCREENWIDTH, 10)];
            separateLabel.backgroundColor = [UIColor colorWithHexString:@"f5f8fa"];
            [tBack addSubview:separateLabel];
            
            leftButton = [UIButton buttonWithType:UIButtonTypeCustom];
            leftButton.frame =CGRectMake(0, 10, SCREENWIDTH/2, 44);
            leftButton.titleLabel.font = [UIFont systemFontOfSize:16];
            [leftButton setTitle:@"日统计" forState:UIControlStateNormal];
            [leftButton setTitleColor:DEFAULTCOLOR forState:UIControlStateNormal];

            [leftButton addTarget:self action:@selector(leftAction:) forControlEvents:UIControlEventTouchUpInside];

            [tBack addSubview:leftButton];
            
            rightButton = [UIButton buttonWithType:UIButtonTypeCustom];
            rightButton.frame =CGRectMake(SCREENWIDTH/2, 10, SCREENWIDTH/2, 44);
            rightButton.titleLabel.font = [UIFont systemFontOfSize:16];
            [rightButton setTitle:@"月统计" forState:UIControlStateNormal];
            [rightButton setTitleColor:[UIColor colorWithHexString:@"3b3b3b"] forState:UIControlStateNormal];
            

            [rightButton addTarget:self action:@selector(rightAction:) forControlEvents:UIControlEventTouchUpInside];
            [tBack addSubview:rightButton];
            
            if (isdayAcount == 1) {
                
                scrollLine = [[UIImageView alloc]initWithFrame:CGRectMake(0, 52, SCREENWIDTH/2, 2)];
                [leftButton setTitleColor:DEFAULTCOLOR forState:UIControlStateNormal];
                [rightButton setTitleColor:[UIColor colorWithHexString:@"3b3b3b"] forState:UIControlStateNormal];
            }else
            {
                scrollLine = [[UIImageView alloc]initWithFrame:CGRectMake(SCREENWIDTH/2, 52, SCREENWIDTH/2, 2)];
                [leftButton setTitleColor:[UIColor colorWithHexString:@"3b3b3b"] forState:UIControlStateNormal];
                [rightButton setTitleColor:DEFAULTCOLOR forState:UIControlStateNormal];
            }
            
            scrollLine.backgroundColor = DEFAULTCOLOR;
            [tBack addSubview:scrollLine];
            
            UILabel * line = [[UILabel alloc]initWithFrame:CGRectMake(0, 53.5, SCREENWIDTH, 0.5)];
            line.backgroundColor = [UIColor colorWithHexString:@"dedede"];
            [tBack addSubview:line];
            
            dataLabel = [[UILabel alloc]initWithFrame:CGRectMake(SCREENWIDTH/2-50, CGRectGetMaxY(line.frame)+10, 100, 15)];
            
            if (isdayAcount == 1) {
                
                dataLabel.text = selectedDataTime;
                
            }else
            {
                dataLabel.text = selectedMonthTime;
            }
            
            CGSize  size = [dataLabel.text boundingRectWithSize:CGSizeMake(1000, 20)  options:(NSStringDrawingUsesFontLeading|NSStringDrawingTruncatesLastVisibleLine|NSStringDrawingUsesLineFragmentOrigin)   attributes:@{NSFontAttributeName: [UIFont systemFontOfSize:15]} context:nil].size;
            
            
            dataLabel.frame = CGRectMake(SCREENWIDTH/2-size.width/2, CGRectGetMaxY(line.frame)+10, size.width, 20);
            dataLabel.font = [UIFont systemFontOfSize:15];
            dataLabel.textColor = [UIColor colorWithHexString:@"3b3b3b"];
            dataLabel.textAlignment = NSTextAlignmentCenter;
            [tBack addSubview:dataLabel];
            
            UIButton * reduceButton = [UIButton buttonWithType:UIButtonTypeCustom];
            reduceButton.frame = CGRectMake(SCREENWIDTH/2-dataLabel.frame.size.width/2-50, CGRectGetMaxY(line.frame), 50, 40);
            [reduceButton setImageEdgeInsets:UIEdgeInsetsMake(0, 23, 0, 0)];
            [reduceButton setImage:[UIImage imageNamed:@"arrow_left_"] forState:UIControlStateNormal];
            [reduceButton addTarget: self action:@selector(reduceAction:) forControlEvents:UIControlEventTouchUpInside];
            [tBack addSubview:reduceButton];
            
            UIButton * addButton = [UIButton buttonWithType:UIButtonTypeCustom];
            addButton.frame = CGRectMake(SCREENWIDTH/2+dataLabel.frame.size.width/2, CGRectGetMaxY(line.frame), 50, 40);
            [addButton setImageEdgeInsets:UIEdgeInsetsMake(0, 0, 0, 23)];
            [addButton setImage:[UIImage imageNamed:@"arrow_right_"] forState:UIControlStateNormal];
            [addButton addTarget: self action:@selector(addAction:) forControlEvents:UIControlEventTouchUpInside];
            [tBack addSubview:addButton];
            
            
            UILabel * line2 = [[UILabel alloc]initWithFrame:CGRectMake(0, CGRectGetMaxY(reduceButton.frame), SCREENWIDTH, 0.5)];
            line2.backgroundColor = [UIColor colorWithHexString:@"dedede"];
            [tBack addSubview:line2];
            
            UILabel * nameLable = [[UILabel alloc]initWithFrame:CGRectMake(0, CGRectGetMaxY(line2.frame)+12, SCREENWIDTH/2, 16)];
            nameLable.text = @"姓名";
            nameLable.font = [UIFont systemFontOfSize:16];
            nameLable.textColor = [UIColor colorWithHexString:@"3b3b3b"];
            nameLable.textAlignment = NSTextAlignmentCenter;
            [tBack addSubview:nameLable];
            
            UILabel * timeLable = [[UILabel alloc]initWithFrame:CGRectMake(SCREENWIDTH/2, CGRectGetMaxY(line2.frame)+12, SCREENWIDTH/2, 16)];
            timeLable.text = @"时间";
            timeLable.font = [UIFont systemFontOfSize:16];
            timeLable.textColor = [UIColor colorWithHexString:@"3b3b3b"];
            timeLable.textAlignment = NSTextAlignmentCenter;
            [tBack addSubview:timeLable];
            
            return tBack;
        }
    
    
    
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
        if (indexPath.section == 0) {
            
            ClassTopTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"ClassTopCell"];
            if (cell == nil) {
                
                cell = [[ClassTopTableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"ClassTopCell"];
            }
            cell.selectionStyle = UITableViewCellSelectionStyleNone;
            
            NSString * monthStr = [self getCurrentMonthString];
            
            if (monthStr.length > 5 ) {
                
                monthStr = [monthStr substringFromIndex:5];
                
                if (monthStr.length > 1) {
                    
                    currentmonth = [monthStr substringToIndex:1];
                    
                    if([currentmonth isEqualToString:@"0"])
                    {
                        currentmonth = [monthStr substringFromIndex:1];
                    }else
                    {
                        currentmonth = monthStr;
                    }
                }
            }
            
            cell.markLabel.text = [NSString stringWithFormat:@"%@月勤奋榜",currentmonth];
            
            NSLog(@"self.rankArray = %@",self.rankArray);
            
            [cell setDataForCell:self.rankArray];
            
            [cell.rankButton addTarget:self action:@selector(pushRankListAction:) forControlEvents:UIControlEventTouchUpInside];
            return cell;
            
        }else
        {
            AccountListTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"AccountListcell"];
            
            if (cell == nil) {
                cell = [[AccountListTableViewCell alloc]initWithStyle:(UITableViewCellStyleDefault) reuseIdentifier:@"AccountListcell"];
                
            }
            
            RankModel * model =[self.dataArray objectAtIndex:indexPath.row];
            
            cell.numlab.text = [NSString stringWithFormat:@"%ld",(long)indexPath.row +1];
            cell.nameLabel.text = model.username;
            
            if (model.jointime.length>11) {
                
               cell.timeLabel.text = [model.jointime substringFromIndex:11];
            }
           
            cell.selectionStyle = UITableViewCellSelectionStyleNone;
            return cell;
        }
}



- (void)pushRankListAction :(UIButton *)sender
{
    FR_RankListViewController * rankListVC = [[FR_RankListViewController alloc]init];
//    rankListVC.rankArr = self.rankArray;
    rankListVC.classID = selectedClassId;
    rankListVC.CurrentClassName = className;
    rankListVC.flag = 1;
    [self.navigationController pushViewController:rankListVC animated:YES];
}

#pragma ButtonAction

- (void)leftAction :(UIButton *)sender
{
    daysdis = 0;


    if (isdayAcount != 1) {
        
        dataLabel.text = [self getCurrentData];
        pageNum = 1;
        [self requstDateRankData];
    }
    isdayAcount = 1;
    [rightButton setTitleColor:[UIColor colorWithHexString:@"3b3b3b"] forState:UIControlStateNormal];
    [leftButton setTitleColor:DEFAULTCOLOR forState:UIControlStateSelected];
    scrollLine.frame = CGRectMake(0, 52, SCREENWIDTH/2, 2);
    
    
}

- (void)rightAction :(UIButton *)sender
{
    
    monthdis = 0;
    
    if (isdayAcount != 2) {
        
        [self getCurrentMonth];
        MpageNum = 1;
        [self requstMonthRankData];
    }
    
    isdayAcount = 2;
    [leftButton setTitleColor:[UIColor colorWithHexString:@"3b3b3b"] forState:UIControlStateNormal];
    [rightButton setTitleColor:DEFAULTCOLOR forState:UIControlStateSelected];
    scrollLine.frame = CGRectMake(SCREENWIDTH/2, 52, SCREENWIDTH/2, 2);

    
}

- (void)reduceAction :(UIButton *)sender
{
    if (isdayAcount == 1) {
        
        [self getLastData];
        [self requstDateRankData];
    }else
    {
        [self getLastMonth];
        [self requstMonthRankData];
    }
}

- (void)addAction :(UIButton *)sender
{
    if (isdayAcount == 1) {
        
        [self getNextData];
        [self requstDateRankData];
    }else
    {
        [self getnextMonth];
        [self requstMonthRankData];
    }
    
}

- (NSString * )getCurrentData
{
    NSDate *currentDate = [NSDate date];//获取当前时间，日期
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:@"YYYY-MM-dd"];
    NSString *dateString = [dateFormatter stringFromDate:currentDate];
    selectedDataTime = dateString;
    return dateString;
}

- (void)getLastData
{
    daysdis --;
    
    NSDate * nowDate = [NSDate date];
    
    NSDateFormatter* formater = [[NSDateFormatter alloc] init];
    
    [formater setDateFormat:@"YYYY-MM-dd"];
    
    NSTimeInterval  oneDay = 24*60*60*1;  //1天的长度
    
    NSDate *lastDay;
    

    lastDay = [NSDate dateWithTimeInterval:+oneDay*daysdis sinceDate:nowDate];
    
    
    NSString *day = [formater stringFromDate:lastDay];
    
    selectedDataTime = day;
    
    dataLabel.text = day;
}

- (void)getNextData
{
    daysdis ++;
    
    NSDate * nowDate = [NSDate date];
    
    NSDateFormatter* formater = [[NSDateFormatter alloc] init];
    
    [formater setDateFormat:@"YYYY-MM-dd"];
    
    NSTimeInterval  oneDay = 24*60*60*1;  //1天的长度
    
    NSDate *nextDay = [NSDate dateWithTimeInterval:+oneDay*daysdis sinceDate:nowDate];

    NSString *day = [formater stringFromDate:nextDay];
    
    selectedDataTime = day;
    
    dataLabel.text = day;

}

- (void)getCurrentMonth
{
    NSCalendar *calender = [NSCalendar currentCalendar];//获取NSCalender单例。
    NSDateComponents *cmp = [calender components:(NSCalendarUnitMonth | NSCalendarUnitYear) fromDate:[NSDate date]]; //设置属性，因为我只需要年和月，这个属性还可以支持时，分，秒。
    NSDate *currentMonDate = [calender dateFromComponents:cmp];
    
    NSDateFormatter* formater = [[NSDateFormatter alloc] init];
    
    [formater setDateFormat:@"YYYY-MM"];
    
    NSString *month = [formater stringFromDate:currentMonDate];
    
    selectedMonthTime = month;
    
    dataLabel.text = month;
    
}

- (void)getLastMonth
{
    monthdis--;
    NSCalendar *calender = [NSCalendar currentCalendar];//获取NSCalender单例。
    NSDateComponents *cmp = [calender components:(NSCalendarUnitMonth | NSCalendarUnitYear) fromDate:[NSDate date]]; //设置属性，因为我只需要年和月，这个属性还可以支持时，分，秒。
    [cmp setMonth:[cmp month]+monthdis];
    
    NSDate *lastMonDate = [calender dateFromComponents:cmp];
    
    NSDateFormatter* formater = [[NSDateFormatter alloc] init];
    
    [formater setDateFormat:@"YYYY-MM"];
    
    NSString *month = [formater stringFromDate:lastMonDate];
    
    selectedMonthTime = month;
    
    dataLabel.text = month;
}

- (void)getnextMonth
{
    monthdis++;
    NSCalendar *calender = [NSCalendar currentCalendar];//获取NSCalender单例。
    NSDateComponents *cmp = [calender components:(NSCalendarUnitMonth | NSCalendarUnitYear) fromDate:[NSDate date]]; //设置属性，因为我只需要年和月，这个属性还可以支持时，分，秒。
    [cmp setMonth:[cmp month]+monthdis];
    
    NSDate *nextMonDate = [calender dateFromComponents:cmp];
    
    NSDateFormatter* formater = [[NSDateFormatter alloc] init];
    
    [formater setDateFormat:@"YYYY-MM"];
    
    NSString *month = [formater stringFromDate:nextMonDate];
    
    selectedMonthTime = month;
    
    dataLabel.text = month;
}

#pragma dataSource

- (void)requestClasses {
    
    NSMutableDictionary * paramDic = [[NSMutableDictionary alloc] initWithCapacity:0];
    [paramDic setObject:USERID forKey:@"teacherid"];
    [paramDic setObject:SCHOOLID forKey:@"scid"];
    [paramDic setObject:[SYAccountTool ShareAccount].account.schoolManage > 0?@"1":@"0" forKey:@"type"];
    
    [Utils Java_get:@"api/getTeacherClass" parameters:paramDic success:^(NSInteger recode, id jsonObject) {
        
        NSArray *array = [jsonObject objectForKey:@"re_data"];
        NSLog(@"array = %@",array);
        
        
        if (array.count == 0) {
            
            UIAlertView * alert = [[UIAlertView alloc]initWithTitle:nil message:@"您还有没班主任的班" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
            [alert show];
        }else
        {
            self.dataArr = [ManagerInfoModel mj_objectArrayWithKeyValuesArray:array];
            
            for (int i = 0 ; i<self.dataArr.count; i++) {
                
                ManagerInfoModel * model =[self.dataArr objectAtIndex:i];
                [self.classnameArr addObject:model.classname];
                [self.classidArr addObject:model.classid];
            }
            
            
            selectedClassId = [self.classidArr objectAtIndex:0];
            className = [self.classnameArr objectAtIndex:0];
            [self setupUI];
            [self requestRankingData];//请求排名数据
            [self requstDateRankData];
            
        }
        NSLog(@"self.dataArr = %@,self.classnameArr = %@ self.classidArr = %@",self.dataArr,self.classnameArr,self.classidArr);
        
    } failure:^(NSInteger recode, NSError *error) {
        [self showHint:error.localizedDescription];
    }];
    
    /*
    RequestParam *param = [[RequestParam alloc] init];
    param.method = @"sy_rollcall_classlist";
    param.pageno = @"1";
    param.pagesize  = @"10000";
    param.teacherid = USERID;
    [Utils get:nil parameters:param.mj_keyValues success:^(NSInteger recode, id jsonObject) {
        
        NSArray *array = [jsonObject objectForKey:@"re_data"];
        NSLog(@"array = %@",array);
        
        
        if (array.count == 0) {
            
            UIAlertView * alert = [[UIAlertView alloc]initWithTitle:nil message:@"您还有没班主任的班" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
            [alert show];
        }else
        {
            self.dataArr = [ManagerInfoModel mj_objectArrayWithKeyValuesArray:array];
            
            for (int i = 0 ; i<self.dataArr.count; i++) {
                
                ManagerInfoModel * model =[self.dataArr objectAtIndex:i];
                [self.classnameArr addObject:model.classname];
                [self.classidArr addObject:model.classid];
            }
            
            
            selectedClassId = [self.classidArr objectAtIndex:0];
            className = [self.classnameArr objectAtIndex:0];
            [self setupUI];
            [self requestRankingData];//请求排名数据
            [self requstDateRankData];
            
        }
        NSLog(@"self.dataArr = %@,self.classnameArr = %@ self.classidArr = %@",self.dataArr,self.classnameArr,self.classidArr);
        
    } failure:^(NSInteger recode, NSError *error) {
        
        [self showHint:error.localizedDescription];
    }];
     */
}

-(void)requestRankingData
{
    
    RequestParam *param = [[RequestParam alloc] init];
    param.classid = selectedClassId;
    param.scid = [NSString stringWithFormat:@"%lld",[SYAccountTool ShareAccount].account.schoolManage];
    param.datetime = [NSString stringWithFormat:@"%@-01",[self getCurrentMonthString]];
    
    [Utils Java_get:@"api/getMonthCountTop" parameters:param.mj_keyValues success:^(NSInteger recode, id jsonObject) {
        
        NSArray *array = [jsonObject objectForKey:@"re_data"];
        NSLog(@"rankArray = %@%@",array,jsonObject);
        
        [self.rankArray removeAllObjects];
        
        if (array.count>0) {
            
            for (NSDictionary * dic in array) {
                
                RankModel * model = [RankModel mj_objectWithKeyValues:dic];
                [self.rankArray addObject:model];
            }
        }
        
        [self.classTable reloadData];
        
    }failure:^(NSInteger recode, NSError *error) {
        [self showHint:error.localizedDescription];
    }];
    
    /*
    RequestParam *param = [[RequestParam alloc] init];
    param.method = @"api_face_ranking";
    param.classid = selectedClassId;
    param.datetime = [NSString stringWithFormat:@"%@-01",[self getCurrentMonthString]];
    [Utils get:nil parameters:param.mj_keyValues success:^(NSInteger recode, id jsonObject) {
        
        NSArray *array = [jsonObject objectForKey:@"re_data"];
        NSLog(@"rankArray = %@%@",array,jsonObject);
        
        [self.rankArray removeAllObjects];
        
        if (array.count>0) {
            
            for (NSDictionary * dic in array) {
                
                RankModel * model = [RankModel mj_objectWithKeyValues:dic];
                [self.rankArray addObject:model];
            }
        }
        
        [self.classTable reloadData];
        
    } failure:^(NSInteger recode, NSError *error) {
        
        [self showHint:error.localizedDescription];
    }];
     */
}
- (void)requstDateRankData
{
    
    NSMutableDictionary *paramDic = [[NSMutableDictionary alloc]init];
    
    [paramDic setObject:[NSString stringWithFormat:@"%@",dataLabel.text] forKey:@"datetime"];
    [paramDic setObject:[NSString stringWithFormat:@"%lld",[SYAccountTool ShareAccount].account.schoolManage] forKey:@"scid"];
    [paramDic setObject:selectedClassId forKey:@"classid"];
    
    [Utils Java_get:@"api/getDailyRankList" parameters:paramDic success:^(NSInteger recode, id jsonObject) {
        
        NSArray *array = [jsonObject objectForKey:@"re_data"];
        
        NSLog(@"array = %@%@",array,jsonObject);
        
        [self.dataArray removeAllObjects];
        
        noDataLabel.hidden = YES;
        
        if (array.count > 0) {
            
            for (NSDictionary * dic in array) {
                
                RankModel * model = [RankModel mj_objectWithKeyValues:dic];
                [self.dataArray addObject:model];
            }
            
            
        }else
        {
            [self noDataLabel];
        }
        
        [self.classTable reloadData];
        
    } failure:^(NSInteger recode, NSError *error) {
        
        [self showHint:error.localizedDescription];
    }];
    
    /*
    RequestParam *param = [[RequestParam alloc] init];
    param.method = @"api_face_rankingdate";
    param.classid = selectedClassId;
    param.datetime = [NSString stringWithFormat:@"%@",dataLabel.text];
    param.pageno = @"1";
    param.pagesize = @"2000";
    
    [Utils get:nil parameters:param.mj_keyValues success:^(NSInteger recode, id jsonObject) {
        
        NSArray *array = [jsonObject objectForKey:@"re_data"];
        
        NSLog(@"array = %@%@",array,jsonObject);
        
        [self.dataArray removeAllObjects];
        
        noDataLabel.hidden = YES;
        
        if (array.count > 0) {
            
            for (NSDictionary * dic in array) {
                
                RankModel * model = [RankModel mj_objectWithKeyValues:dic];
                [self.dataArray addObject:model];
            }
            
            
        }else
        {
            [self noDataLabel];
        }
        
        [self.classTable reloadData];
        
        
    } failure:^(NSInteger recode, NSError *error) {
        
        [self showHint:error.localizedDescription];
    }];
     */
}

- (void)requstMonthRankData
{
    
    NSMutableDictionary *paramDic = [[NSMutableDictionary alloc]init];
    
    //todo [paramDic setObject:[NSString stringWithFormat:@"%lld",[SYAccountTool ShareAccount].account.schoolManage] forKey:@"scid"];
    [paramDic setObject:[NSString stringWithFormat:@"%@-01",dataLabel.text] forKey:@"datetime"];
    [paramDic setObject:selectedClassId forKey:@"classid"];
    [paramDic setObject:[SYAccountTool ShareAccount].account.scid forKey:@"scid"];
    
    [Utils Java_get:@"api/getMonthAvgTimeTop" parameters:paramDic success:^(NSInteger recode, id jsonObject) {
        
        NSArray *array = [jsonObject objectForKey:@"re_data"];
        
        NSLog(@"array = %@%@",array,jsonObject);
        
        [self.dataArray removeAllObjects];
        
        noDataLabel.hidden = YES;
        
        if (array.count > 0 ) {
            
            for (NSDictionary * dic in array) {
                
                RankModel * model = [RankModel mj_objectWithKeyValues:dic];
                [self.dataArray addObject:model];
            }
            
            
        }else
        {
            [self noDataLabel];
        }
        
        [self.classTable reloadData];
        
    } failure:^(NSInteger recode, NSError *error) {
        
        [self showHint:error.localizedDescription];
    }];
    
    /*
    RequestParam *param = [[RequestParam alloc] init];
    param.method = @"api_face_rankingmonth";
    param.classid = selectedClassId;
    param.datetime = [NSString stringWithFormat:@"%@-01",dataLabel.text];
    param.pageno = @"1";
    param.pagesize = @"2000";
    
    [Utils get:nil parameters:param.mj_keyValues success:^(NSInteger recode, id jsonObject) {
        
        NSArray *array = [jsonObject objectForKey:@"re_data"];
        
        NSLog(@"array = %@%@",array,jsonObject);
        
        [self.dataArray removeAllObjects];
        
         noDataLabel.hidden = YES;
        
        if (array.count > 0 ) {
            
            for (NSDictionary * dic in array) {
                
                RankModel * model = [RankModel mj_objectWithKeyValues:dic];
                [self.dataArray addObject:model];
            }

            
        }else
        {
            [self noDataLabel];
        }
        
        [self.classTable reloadData];
        
    } failure:^(NSInteger recode, NSError *error) {
        
        [self showHint:error.localizedDescription];
    }];
     */
}

- (NSString *)getCurrentMonthString
{
    NSCalendar *calender = [NSCalendar currentCalendar];//获取NSCalender单例。
    NSDateComponents *cmp = [calender components:(NSCalendarUnitMonth | NSCalendarUnitYear) fromDate:[NSDate date]]; //设置属性，因为我只需要年和月，这个属性还可以支持时，分，秒。
    NSDate *currentMonDate = [calender dateFromComponents:cmp];
    
    NSDateFormatter* formater = [[NSDateFormatter alloc] init];
    
    [formater setDateFormat:@"YYYY-MM"];
    
    NSString *month = [formater stringFromDate:currentMonDate];
    
    return month;
    
}
-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    [self.navigationController popViewControllerAnimated:YES];
}
- (void)noDataLabel
{
    noDataLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, (SCREENHEIGH + 252)/2, SCREENWIDTH, 15)];
    noDataLabel.text = @"暂无统计";
    noDataLabel.font = [UIFont systemFontOfSize:15];
    noDataLabel.textColor = RGBCOLOR(151, 151, 151);
    noDataLabel.textAlignment = NSTextAlignmentCenter;
    [self.view addSubview:noDataLabel];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)viewWillDisappear:(BOOL)animated
{
   NSLog(@"rankArray2222 = %@",self.rankArray);
}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
